"""
正则表达式，re模块，re.match(正则表达式，待匹配字符串)，正则表达式匹配字符串过程中，遇到不在表达式内的字符串，就会停止匹配
"""
import re

#"."，匹配任意一个字符，除了\n
def PP_anything():
    ret =re.match('.','s')
    print(f'{ret.group()}')

def PP_fkh():
#'[]'匹配[]中列举的字符
    ret1 =re.match("[a-z]",'abc')
    ret2 =re.match("[a]",'abs')
    ret3 =re.match("[hH]",'hello')
    ret4 =re.match("[hH]",'Hello hello')
    ret5 =re.match("[hH]ello",'hello')
    ret6 =re.match('[0-9]','32ajd')
    ret7 =re.match('[0-35-9]','42ajd')
    print(f'{ret2.group()},{ret1.group()},{ret3.group()},{ret4.group()},{ret5.group()},{ret6.group()},{ret7.group()}')

def PP_num():
    #"\d"，匹配数字  "\D"，匹配非数字
    ret8 =re.match('ab3jk','ab3jkljj')
    ret9 =re.match(r'ab\djk','ab3jkljj')
    ret10 =re.match(r'2\D3','2j3')
    ret11 =re.match(r'2\D3','2#3')
    print(f'{ret8.group()},{ret9.group()},{ret10.group()},{ret11.group()}')

def PPs_S():
    #"\s"，匹配空格  "\S"，匹配非空格
    ret12 =re.match(r'2\s3','2 3')
    ret13 =re.match(r'\S3','#3')
    print(f'{ret12.group()},{ret13.group()}')

def PPw_W():
    #"\w"，匹配单词字符  "\W"，匹配非单词字符
    ret14 =re.match(r'2\w3','2_3')  #\w可匹配a-z，A-Z，0-9，_（汉字）word
    ret15 =re.match(r'2\w3','2式3')
    ret16 =re.match(r'2\W3','2#3')
    ret17 =re.match(r'2\W3','2 3')
    print(f'{ret14.group()},{ret15.group()},{ret16.group()},{ret17.group()}')

def PP_mur1():
    #"*"匹配前一个字符出现0次或无限次
    ret18 =re.match(r'[A-Z][a-z]*','AbbbbA')
    ret19 =re.match(r'[A-Z]*[a-z]*','AAAASDAafdaA')
    print(f'{ret18.group()},{ret19.group()}')

def PP_mur2():
    #"+"匹配前一个字符出现1次或无限次
    names =['afdas123','2_afds3','_aafd','__adaf__']
    for name in names:
        ret20 =re.match(r'[a-zA-Z_]+[\w]*',name)
        if ret20:
            print(f'变量名{ret20.group()}符合要求')
        else:
            print(f'变量名{name}非法')

def PP_1or0():
    #"?"，匹配前一个字符出现1次或0次
    ret21 =re.match(r'[a-z]?','ava')
    ret22 =re.match(r'[a-z]?[A-Z]?','aAA')  #aA
    ret24 =re.match(r'[a-z]?[A-Z]?','abAA')  #a
    ret23 =re.match(r'[0-9]?\d','321')
    print(f'{ret21.group()},{ret22.group()},{ret23.group()},{ret24.group()}')

def PP_m():
    #"{m}" 匹配前一个字符出现m次
    #"{m，n}"匹配前一个字符出现从m到n次
    ret25 =re.match(r'[a-zA-Z0-9_]{8,20}','avad23_23AFDSdasfasdfsaf')   #avad23_23AFDSdasfasd
    ret26 =re.match(r'[a-z]{3}','ava')
    print(f'{ret25.group()},{ret26.group()}')

    #匹配163的邮箱地址，且@符号之前有4到20位
    #"$"匹配字符串结尾
    # "^"匹配字符串开头
    miles_163 =['3123afaAFA@163.com','com.ajf12A#@163.com','af_a12312@163.com',
           'add@163.com','afsdhfkashfkhkf2323233@163.com','3123afaAFA@163.comhkj',
           '3123afaAFA@qq.com']
    for mile in miles_163:
        ret27 =re.match(r'[0-9a-zA-Z]{4,20}@163\.com$',mile)
        if ret27:
            print(f'邮箱名{ret27.group()}符合要求')
        else:
            print(f'邮箱名{mile}非法')

def PP_left_right():
# "|" 匹配左右任意一个表达式
    #匹配出0-100的数字

    ret28 =re.match(r'[1-9]?\d','7')
    ret29 =re.match(r'[1-9]?[0-9]?','63')
    ret30 =re.match(r'[0-9]?\d','07')
    ret31 =re.match(r'[0-9]?[\d]+','077')
    ret32 =re.match(r'[1-9]?\d$|100','07')
    if ret32:
        print(f'{ret32.group()}')
    else:
        print(f'不在0-100之间')
    print(f'{ret28.group()},{ret29.group()},{ret30.group()},{ret31.group()}')

def PP_group():
#"(ab)" 将括号中字符作为一个分组
    #匹配出163，126，qq邮箱
    mails=['10086as@qq.com','abcde23@163.com','asdhfa3@126.com']
    for mail in mails:
        ret33 =re.match(r'\w{4,20}@(163|126|qq)\.com',mail)
        if ret33:
            print(f'{ret33.group()}')
        else:
            print(f'{mail}邮箱名不正确')

    #提取区号和电话号码
    ret34 = re.match(r'([^-]*)-(\d*)','010-12321321')    #两个 () 是两个分组，分组可以分别输出打印
    print(f'{ret34.group()},{ret34.group(1)},{ret34.group(2)}')

def PP_cite_group():
    # "\num" 引用分组num匹配到的字符串
    #匹配出<html>hh</html>
    ret35 = re.match(r'<\w{4}>\w*</\w{4}>','<html>hh</html>')
    ret36 = re.match(r'<([\w]*)>\w*<(/\1)>','<html>hh</html>')   #\1第一个（）中的内容，两者匹配内容必须相同
    print(f'{ret35.group()},{ret36.group()}')

    #匹配出<html><h1>www.cskaoyan.com</h1></html>
    labels =['<html><h1>www.cskaoyan.com</h1></html>','<html><h1>www.cskaoyan.com</h2></html>']
    for label in labels:
        ret37 = re.match(r'<([\w]*)><([\w]*)>.*</\2></\1>',label)
        if ret37:
            print(f'{ret37.group()}是合法标签')
        else:
            print(f'{label}不合法')

def PP_rename():
    # "(?p<name>)" 分组起别名,"(?P=name)" 引用别名
    # #匹配出<html><h1>www.cskaoyan.com</h1></html>
    ret38 =re.match(r'<(?P<w3>\w*)><(?P<w9>\w*)>.*</(?P=w9)></(?P=w3)>','<html><h1>www.cskaoyan.com</h1></html>')
    print(f'{ret38.group()}')

if __name__ == '__main__':
    PP_anything()
    PP_fkh()
    PP_num()
    PPw_W()
    PPs_S()
    PP_1or0()
    PP_mur2()
    PP_mur1()
    PP_m()
    PP_left_right()
    PP_group()
    PP_cite_group()
    PP_rename()






